﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using ThirdServices.AlertModel.Factory.Entities.DTO;
using ThirdServices.TemperatureSettings;

namespace ThirdServices.AlertModel.Analyzers
{
    class TempAnalyser
    {
        public static List<BaseAlert> AnalyseTemperatureData(List<TemperatureData> tempData, double delta,int periodInDays,int numDataPerDay)
        {
            double num1,num2,temp;
            List<BaseAlert> baseAlerts = new List<BaseAlert>();
            for (int i = 0; i < tempData.Count - numDataPerDay * periodInDays; i++)
            {
                for (int j = i; j < tempData.Count; j += numDataPerDay)
                {
                    num1 = tempData[i].Temperature;
                    num2 = tempData[j].Temperature;
                    if (num2 < num1)
                    {
                        temp = num1;
                        num1 = num2;
                        num2 = temp;
                    }
                    if (Math.Abs(num1*100/num2 - 100) > delta)
                    {
                        baseAlerts.Add(new BaseAlert
                                           {
                                               NotificationDescription = new NotificationDescription
                                                                             {
                                                                                 alert_type =
                                                                                     AlertTypes.TempAlert,
                                                                                 notification =
                                                                                     Convert.ToString(tempData[i].Date,
                                                                                                      CultureInfo.
                                                                                                          InvariantCulture) +
                                                                                     " - " +
                                                                                     Convert.ToString(tempData[j].Date,
                                                                                                      CultureInfo.
                                                                                                          InvariantCulture) +
                                                                                     " , " +
                                                                                     Convert.ToString(delta,
                                                                                                      CultureInfo.
                                                                                                          InvariantCulture) + "%",
                                                                                 date = Convert.ToDateTime(tempData[i].Date,
                                                                                                        CultureInfo.InvariantCulture),
                                                                                 value = Convert.ToDouble(delta,CultureInfo.InvariantCulture),
                                                                                 measure = Measures.percent
                                                                             }
                                           });
                        break;
                    }
                }
            }
            /*for (int i = 0; i < tempData.Count;i++ )
            {
                for(int j=i;j<tempData.Count;j++)
                {
                    if (Math.Abs(tempData[i].Temperature - tempData[j].Temperature) > delta)
                    {
                        baseAlerts.Add(new BaseAlert
                                           {
                                               NotificationDescription = new NotificationDescription
                                                                             {
                                                                                 alert_type =
                                                                                     Enum.GetName(typeof (Alerts),
                                                                                                  Alerts.TempAlert),
                                                                                 notification = Convert.ToString(tempData[i].Date, CultureInfo.InvariantCulture) + " - " + Convert.ToString(tempData[j].Date, CultureInfo.InvariantCulture) + " , " + Convert.ToString(delta,CultureInfo.InvariantCulture)
                                                                             }
                                           });
                        break;
                    }
                }
            }*/
            return baseAlerts;
        }
    }
}
